Introduction to JavaScript (Volume 1) (Web Programming for Beginners Series) by Randall Robertson
Author:Randall Robertson [Robertson, Randall]
Language: eng
Format: azw3
Publisher: UNKNOWN
Published: 2018-12-06T16:00:00+00:00
Chapter 11
JavaScript Conditional Statements: Part 2
11.1 The if…else statement The if statement executes a statement if a specified condition is true. If you want to execute some code if a condition is true and another code if the condition is not true, use the if....else statement. Use the else statement to indicate the block of code that should be executed if the condition is false.
if ( condition) {
block of code to be executed if the condition is true
} else {
block of code to be executed if the condition is false
}
Consider the following example: If the age of the user is less than 21, create a message that says: "You're too young to drink." and assign it to a variable called message. If the condition is false, create a message that says: "You're old enough to drink." and assign it to a variable called message.
if (age < 21) {
message = "You're too young to drink."; document.write (message);
} else {
message = "Your old enough to drink"; document.write (message);
}
The result dislpayed in the browser will be:
if condition is true: You're too young to drink. if condition is false: You're old enough to drink.
Syntax for if…else statement:
if (condition)
statement1
else
statement2
condition
An expression, usually an arithmetic or Boolean comparison, that evaluates to either true or false. statement1
statement1 is a block of code that is executed if condition evaluates to true. The statement can contain any valid JavaScript code, including more nested if statements. If you want to execute more than one statement, use a block statement with curly braces ({ ... }) to group these statements together as one execution block. If you don't want to execute any statements, use an empty statement.
statement2
statement2 is a block of code that is executed if condition evaluates to false. The statement can contain any valid JavaScript code, including more nested if statements. If you want to execute more than one statement, use a block statement with curly braces ({ ... }) to group these statements together as one execution block. If you don't want to execute any statements, use an empty statement.
Flowchart of an if…else statement: The condition is an expression that evaluates to either true or false; e.g. number <= 25
condition If the value of number is less than or equal to 25, the condition evaluates to true and this block of code will execute.
If the value of number is greater than 25, the condition evaluates to false and this block of code will execute.
if code else code
after if code This is the code which comes after the if…else statement. This where execution resumes after one of the two blocks of code is executed.
IF…ELSE statement examples: example:
<script>
var name = "Jack";
if (name == "Jim") {
document.write ("Hello Jim."); }
else {
document.write ("Who are you?"); }
</script>
Result displayed in browser: Who are you?
Since the string variable name was initialized with the value "Jack", the comparison name == "Jim" will return false and the code in the if section will be skipped over and the code in the else section will be executed. If the variable
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27093)
Hello! Python by Anthony Briggs(25942)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25284)
Kotlin in Action by Dmitry Jemerov(24393)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23591)
Dependency Injection in .NET by Mark Seemann(23311)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21942)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20847)
Grails in Action by Glen Smith Peter Ledbrook(19869)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17072)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16832)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14464)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12581)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11865)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10650)
Hit Refresh by Satya Nadella(9236)
The Kubernetes Operator Framework Book by Michael Dame(8588)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8443)
Robo-Advisor with Python by Aki Ranin(8386)